package net.sudot.chess.business.controller.web;

import net.sudot.chess.business.controller.AbstractBaseController;
import net.sudot.chess.business.dto.OrderDetailDTO;
import net.sudot.chess.business.model.Customer;
import net.sudot.chess.business.model.OrderRecharge;
import net.sudot.chess.business.model.PaymentTransaction;
import net.sudot.chess.business.model.RechargeConfig;
import net.sudot.chess.business.service.OrderRechargeService;
import net.sudot.chess.business.service.PaymentTransactionService;
import net.sudot.chess.business.service.RechargeConfigService;
import net.sudot.commons.pagination.PageRequest;
import net.sudot.commons.security.LoginUser;
import net.sudot.commons.utils.ResponseMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 充值订单
 *
 * @author tangjialin on 2018-03-22.
 */
@Controller
@RequestMapping(value = "/order/recharge")
public class OrderRechargeController extends AbstractBaseController {

    @Resource
    private OrderRechargeService orderRechargeService;
    @Resource
    private RechargeConfigService rechargeConfigService;
    @Resource
    private PaymentTransactionService paymentTransactionService;

    /**
     * 充值订单视图
     *
     * @return 返回订单视图
     */
    @GetMapping("/index")
    public String index() {
        return "order-recharge/index";
    }

    /**
     * 充值订单视图
     *
     * @param customer 登录用户
     * @param page     分页参数
     * @return 返回订单视图
     */
    @PostMapping("/list")
    @ResponseBody
    public ResponseMessage list(@LoginUser Customer customer, @RequestBody PageRequest page) {
        ResponseMessage responseMessage = new ResponseMessage();
        List<OrderRecharge> list = orderRechargeService.findList(page, customer.getId());
        if (list.size() < page.getRows()) {
            responseMessage.setCode(1);
        }
        return responseMessage.setSuccess(true).setData(list);
    }

    /**
     * 订单详情视图
     *
     * @param orderSn 订单号
     * @return 返回订单编号
     */
    @GetMapping("/detail/{orderSn}")
    public String detailView(@PathVariable("orderSn") String orderSn) {
        return "order-recharge/detail";
    }

    /**
     * 获取订单详情
     *
     * @param customer      登录客户
     * @param orderRecharge 订单信息
     *                      sn:(必须)订单号
     * @return 返回订单编号
     */
    @PostMapping("/detail")
    @ResponseBody
    public ResponseMessage detail(@LoginUser Customer customer, @RequestBody OrderRecharge orderRecharge) {
        ResponseMessage responseMessage = new ResponseMessage();
        if (orderRecharge.getSn() == null) { return responseMessage.setMessage("订单编号不能为空"); }
        orderRecharge = orderRechargeService.findBySn(orderRecharge.getSn());
        if (orderRecharge == null) { return responseMessage.setMessage("订单不存在"); }
        if (!customer.getId().equals(orderRecharge.getUserId())) { return responseMessage.setMessage("订单不存在"); }
        PaymentTransaction paymentTransaction = paymentTransactionService.findByTarget(PaymentTransaction.Type.RECHARGE, orderRecharge.getId());
        return responseMessage.setSuccess(true).setData(new OrderDetailDTO(orderRecharge, paymentTransaction));
    }

    /**
     * 充值订单状态
     *
     * @return 返回订单状态
     */
    @PostMapping("/status")
    @ResponseBody
    public Map<String, String> status() {
        Map<String, String> statusMapping = new HashMap<>();
        for (OrderRecharge.Status status : OrderRecharge.Status.values()) {
            statusMapping.put(status.name(), status.memo());
        }
        return statusMapping;
    }

    /**
     * 创建充值订单
     *
     * @param customer         登录客户
     * @param rechargeConfigId 充值配置
     * @return 返回订单编号
     */
    @PostMapping("/create")
    @ResponseBody
    public ResponseMessage create(@LoginUser Customer customer, Long rechargeConfigId) {
        ResponseMessage responseMessage = new ResponseMessage();
        RechargeConfig rechargeConfig = rechargeConfigId == null ? null : rechargeConfigService.find(rechargeConfigId);
        if (rechargeConfig == null) {
            return responseMessage.setMessage("参数错误");
        }
        OrderRecharge order = orderRechargeService.create(rechargeConfig, customer);
        return responseMessage.setSuccess(true).setData(order.getSn());
    }

    /**
     * 取消订单
     *
     * @param customer      登录客户
     * @param orderRecharge 订单信息
     *                      sn:(必须)订单号
     * @return 返回订单编号
     */
    @PostMapping("/cancel")
    @ResponseBody
    public ResponseMessage cancel(@LoginUser Customer customer, @RequestBody OrderRecharge orderRecharge) {
        ResponseMessage responseMessage = new ResponseMessage();
        if (orderRecharge.getSn() == null) { return responseMessage.setMessage("订单编号不能为空"); }
        orderRecharge = orderRechargeService.findBySn(orderRecharge.getSn());
        if (orderRecharge == null) { return responseMessage.setMessage("订单不存在"); }
        if (!customer.getId().equals(orderRecharge.getUserId())) { return responseMessage.setMessage("订单不存在"); }
        if (orderRecharge.hasExpired() || orderRecharge.getStatus() != OrderRecharge.Status.PENDING_PAYMENT) {
            return responseMessage.setMessage("取消失败,订单已过期或" + orderRecharge.getStatus().memo());
        }
        orderRechargeService.cancel(orderRecharge);
        return responseMessage.setSuccess(true);
    }
}
